import { createStore } from "vuex"
import axios from "axios"
import moduleUser from "./user"
import moduleSearch from "./search"
import moduleSong from "./song"
import modulePlayer from "./player"

const store = createStore({
    state() {
        return {
            // 歌曲链接
            musicSrc: localStorage.getItem("music_src") || '1901371647',
            // 歌曲详情
            musicInfo: JSON.parse(localStorage.getItem("music_info")) || '{}',
            // 播放状态
            isPlay: false
        }
    },
    mutations: {
        // 修改歌曲链接
        updateMusicSrc(state, src) {
            state.musicSrc = src
            localStorage.setItem("music_src", state.musicSrc)
        },
        // 修改歌曲详情
        updateMusicInfo(state, musicInfo) {
            state.musicInfo = musicInfo
            localStorage.setItem("music_info", JSON.stringify(state.musicInfo))
        },
        // 修改播放状态
        updateIsPlay(state, isPlay) {
            state.isPlay = isPlay
        }
    },
    actions: {
        // 获取歌曲详情
        async getMusicInfoData(contest, id) {
            await axios
                .get(`http://localhost:3000/song/detail?ids=${id || contest.state.musicSrc}&realIP=116.25.146.177`)
                .then((res) => {
                    contest.commit("updateMusicInfo", res.data.songs[0]);
                })
                .catch((err) => {
                    console.log(err.message);
                });
        }
    },
    modules: {
        "m_user": moduleUser,
        "m_search": moduleSearch,
        "m_song": moduleSong,
        "m_player": modulePlayer
    }
})

export default store